<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="public/css/style_ontap.css" type="text/css">
<?php
require_once 'model/db.php';
?>


<?php
$query = "SELECT * FROM companies";
$companies = getPages($query);
$querya = "SELECT * FROM apps";
$apps = getCompanies($query);
?>

<div id="form">
    
    <form method="POST">
        <table>
            <tr><td> Min fans</td><td><input type="text" name="min_fans"></td></tr></br>
            <tr><td> Max fans</td><td><input type="text" name="max_fans"></td></tr></br>
            <tr><td> Keyword</td><td> <input type="text" name="keyword"></td></tr></br>
            <tr><td> Company</td><td><select name="id_company">
                        <option value="0"><?php echo "--------Company--------" ?></option>
                        <?php foreach ($companies as $company): ?>

                            <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
                        <?php endforeach; ?>
                    </select></td></tr></br>
            <tr><td> Apps</td><td><select name="id_apps">
                        <option value="0"><?php echo "-------------------App---------------------" ?></option>
                        <?php foreach ($apps as $app): ?>

                            <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                        <?php endforeach; ?>
                    </select></td></tr></br>

        </table>
        <input type="submit" name="submit" value="Search" >
    </form>

</div>
<?php if (isset($_POST['submit'])): ?>

    <?php
    $min_fans = $_POST['min_fans'];
    $max_fans = $_POST['max_fans'];
    $keyword = $_POST['keyword'];
    $company = $_POST["id_company"];
    $apps = $_POST["id_apps"];
    if (!empty($company))
        $company = "companies.id='$company' AND";
    else
        $company = "";
    if (!empty($apps))
        $apps = "apps.id='$apps' AND";
    else
        $apps = "";
    if (empty($max_fans) && !empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  'pages.likes>= $min_fans' and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
    } else if (!empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  'pages.likes<= $max_fans' and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
    } else if (empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
    } else {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  'pages.likes>= $min_fans' and
                  'pages.likes<= $max_fans' and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
    }  
    var_dump($query);
    
    $pages = getCompanies($query);
    ?>
<?php else: ?> 
    <?php
    $query = "SELECT * FROM pages LIMIT 0,15";
    $pages = getPages($query);
    ?>
<?php endif; ?>
<?php if (!empty($pages)): ?>

    <table id="bang" border="1px">
        <tr class="a">
            <td> STT </td>
                <td> Id </td>               
                <td> Name </td>
                <td> Picture </td>
                <td> likes </td>
                <td> Talking </td>
                <td> category </td>
                <td> link </td>
                <td> website </td>
                <td> description </td>
        </tr>
    <?php foreach ($pages as $page): ?>
            <tr class="b">
                <td><?php echo $page["pid"] ?></td>
                    <td><?php echo $page["id"] ?> </td>                    
                    <td><?php echo $page["name"] ?></td>
                    <td><img src="<?php echo $page['picture'] ?>"></td>
                    <td><?php echo $page['likes'] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                    <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                    <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                    <td class="description"><?php echo $page["description"] ?></td>
            </tr>
        <?php endforeach; ?>
    </table>
<?php endif; ?>


